package com.jackchong.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Interpolator;

import com.jackchong.utils.JAuto;;
import com.utils.R;


public class LoadingDialog extends AlertDialog {

    private static final int DELAY = 150;
    private static final int DURATION = 1500;

    private int size = 5;
    private AnimatedView[] spots;
    private AnimatorPlayer animator;


    public LoadingDialog(Context context) {
        super(context, android.R.style.Theme_DeviceDefault_Light_Dialog);
    }

    public LoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        JAuto.auto(this);
        setCanceledOnTouchOutside(false);
        initProgress();
    }

    @Override
    protected void onStart() {
        super.onStart();

        animator = new AnimatorPlayer(createAnimations());
        animator.play();
    }

    @Override
    protected void onStop() {
        super.onStop();

        animator.stop();
    }

    private void initProgress() {

        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(getContext().getResources().getColor(R.color.colorPrimary));
        gradientDrawable.setShape(GradientDrawable.OVAL);

        JFrameLayout progress = (JFrameLayout) findViewById(R.id.progress);
        spots = new AnimatedView[size];
        for (int i = 0; i < spots.length; i++) {
            AnimatedView v = new AnimatedView(getContext());
            v.setBackground(gradientDrawable);
            v.setTarget(JAuto.auto(500));
            v.setXFactor(-1f);
            progress.addView(v, JAuto.auto(15), JAuto.auto(15));
            spots[i] = v;
        }
    }

    private Animator[] createAnimations() {
        Animator[] animators = new Animator[size];
        for (int i = 0; i < spots.length; i++) {
            Animator move = ObjectAnimator.ofFloat(spots[i], "xFactor", 0, 1);
            move.setDuration(DURATION);
            move.setInterpolator(new HesitateInterpolator());
            move.setStartDelay(DELAY * i);
            animators[i] = move;
        }
        return animators;
    }

    public class HesitateInterpolator implements Interpolator {

        private double POW = 1.0/2.0;

        @Override
        public float getInterpolation(float input) {
            return input < 0.5 ? (float) Math.pow(input * 2, POW) * 0.5f : (float) Math.pow((1 - input) * 2, POW) * -0.5f + 1;
        }
    }


    class AnimatedView extends View {

        private int target;

        public AnimatedView(Context context) {
            super(context);
        }

        public float getXFactor() {
            return getX() / target;
        }

        public void setXFactor(float xFactor) {
            setX(target * xFactor - JAuto.auto(15));
        }

        public void setTarget(int target) {
            this.target = target;
        }

        public int getTarget() {
            return target;
        }
    }

     class AnimatorPlayer extends AnimatorListenerAdapter {

        private boolean interrupted = false;
        private Animator[] animators;

        public AnimatorPlayer(Animator[] animators) {
            this.animators = animators;
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (!interrupted) animate();
        }

        public void play() {
            animate();
        }

        public void stop() {
            interrupted = true;
        }

        private void animate() {
            AnimatorSet set = new AnimatorSet();
            set.playTogether(animators);
            set.addListener(this);
            set.start();
        }
    }
}
